package com.intelligent.controller;

import com.intelligent.service.ProcurementWorkbenchService;
import com.intelligent.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 采购工作台控制器
 * 提供采购工作台所需的各种数据统计和查询API
 */
@RestController
@RequestMapping("/procurement")
public class ProcurementController {

    @Autowired
    private ProcurementWorkbenchService procurementWorkbenchService;

    /**
     * 获取采购工作台统计数据
     * 包括采购完成率、订单数、采购金额、供应商数量、库存价值、质检合格率
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getProcurementStatistics() {
        try {
            Map<String, Object> statistics = procurementWorkbenchService.getProcurementStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取采购统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取采购待办事项
     * 包括待审核采购申请、待确认订单、逾期订单、质检异常等
     */
    @GetMapping("/todos")
    public Result<Map<String, Object>> getProcurementTodos() {
        try {
            Map<String, Object> todos = procurementWorkbenchService.getProcurementTodos();
            return Result.success(todos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取采购待办事项失败: " + e.getMessage());
        }
    }

    /**
     * 获取采购趋势数据
     * 包括年度目标、本月目标、采购趋势等
     */
    @GetMapping("/trends")
    public Result<Map<String, Object>> getProcurementTrends() {
        try {
            Map<String, Object> trends = procurementWorkbenchService.getProcurementTrends();
            return Result.success(trends);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取采购趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取采购订单列表
     * @param page 页码
     * @param pageSize 每页数量
     * @param keyword 关键词
     * @param status 订单状态
     * @param expectedDate 期望到货时间
     */
    @GetMapping("/orders")
    public Result<Map<String, Object>> getProcurementOrders(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "") String status,
            @RequestParam(defaultValue = "") String expectedDate) {
        try {
            Map<String, Object> orders = procurementWorkbenchService.getProcurementOrders(
                    page, pageSize, keyword, status, expectedDate);
            return Result.success(orders);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取采购订单列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取供应商列表
     */
    @GetMapping("/suppliers")
    public Result<Map<String, Object>> getSuppliers() {
        try {
            Map<String, Object> suppliers = procurementWorkbenchService.getSuppliers();
            return Result.success(suppliers);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取供应商列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取采购订单详情
     * @param orderId 订单ID
     */
    @GetMapping("/orders/{orderId}")
    public Result<Map<String, Object>> getProcurementOrderDetail(@PathVariable Long orderId) {
        try {
            Map<String, Object> orderDetail = procurementWorkbenchService.getProcurementOrderDetail(orderId);
            return Result.success(orderDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取采购订单详情失败: " + e.getMessage());
        }
    }
}